package InformationRetriever.ComparerMethod;

import Jama.Matrix;

public class CosineComparer extends ComparerBase
{    
    public double PerformCompare(int doc1, int doc2)
    {
        Matrix v = this.getV();
        if(v == null) 
            return 0;
        
        double numerator = 0;
        for(int i = 0; i < v.getRowDimension(); i++)
            numerator += v.get(i,doc1) * v.get(i,doc2);
        
        double sigmaOfSquares1 = 0;
        for(int i = 0; i < v.getRowDimension(); i++)
            sigmaOfSquares1 += v.get(i,doc1) * v.get(i,doc1);
        
        double sigmaOfSquares2 = 0;
        for(int i = 0; i < v.getRowDimension(); i++)
            sigmaOfSquares2 += v.get(i,doc2) * v.get(i,doc2);
        
        return numerator/(Math.sqrt(sigmaOfSquares1) * Math.sqrt(sigmaOfSquares2));
    }
}
